home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktimeintro / graphic import.export / start code / scaleandrotate.c < prev   
Encoding:
Text File  |  2000-10-06  |  2.8 KB  |  95 lines

  1. // Graphics Importer and Exporter Samples
  2. // This example uses graphics importers to draw an image
  3. // then demonstrates how to perform scaling, rotation and perspective
  4. // manipulation using matrix operations
  5. // Originally written by Sam Bushell for QuickTime "Live" '99
  6. // WWDC 2000 Introduction to QuickTime
  7.  
  8. #include "MacShell.h"
  9.  
  10. void ScaleAndRotate( void )
  11. {
  12.     OSErr err = noErr;
  13.     Handle hOpenTypeList = NewHandle(0);
  14.     long     numTypes = 0;
  15.     FSSpec    theFSSpec;
  16.     GraphicsImportComponent importer = 0;
  17.     Rect naturalBounds, windowBounds, scaledBounds;
  18.     WindowPtr window = NULL;
  19.     MatrixRecord matrix;
  20.     Fixed naturalWidth, naturalHeight;
  21.     FixedPoint fromQuad[4], toQuad[4];
  22.  
  23.     BuildGraphicsImporterValidFileTypes( hOpenTypeList, &numTypes );
  24.     HLock( hOpenTypeList );
  25.     
  26.     err = GetOneFileWithPreview(numTypes, (OSTypePtr)*hOpenTypeList, &theFSSpec, NULL);
  27.     DisposeHandle( hOpenTypeList );
  28.     if ( err ) return;
  29.  
  30. // Step 1. Insert Step1.clp here...    
  31.  
  32.     
  33.     pause();
  34.     
  35.     // scale the image by a factor of 2x.  
  36.     // the top-left coordinate of an image's natural bounds is always (0,0).
  37.     scaledBounds = naturalBounds;
  38.     scaledBounds.right = scaledBounds.right * 2;
  39.     scaledBounds.bottom = scaledBounds.bottom * 2;
  40.     
  41.     // define the rectangle in which to draw an image
  42. // Step 2. Insert SetBoundsRect.clp here...
  43.     
  44.     // resize the window to fit, and redraw.
  45. // Step 3. Insert Step3.clp here...
  46.     
  47.     pause();
  48.     
  49.     // rotate the image ninety degrees clockwise
  50. // Step 4. Insert Rotate.clp here...
  51.  
  52.     
  53.     // resize the window to fit, and redraw.
  54.     // get the new bounds rect, size the window then draw
  55. // Step 5. Insert Step5.clp here...
  56.     
  57.     pause();
  58.     
  59.     // give the image some hefty perspective.
  60.     // set up two quadrilateral coordinates and create a translation matrix
  61.     // from one to the other using QuadToQuadMatrix()
  62.     // QuadToQuadMatrix was added in QuickTime 4.0.
  63.     naturalWidth = Long2Fix( naturalBounds.right );
  64.     naturalHeight = Long2Fix( naturalBounds.bottom );
  65.     fromQuad[0].x = 0;
  66.     fromQuad[0].y = 0;
  67.     fromQuad[1].x = naturalWidth;
  68.     fromQuad[1].y = 0;
  69.     fromQuad[2].x = naturalWidth;
  70.     fromQuad[2].y = naturalHeight;
  71.     fromQuad[3].x = 0;
  72.     fromQuad[3].y = naturalHeight;
  73.     toQuad[0].x = naturalWidth/8;
  74.     toQuad[0].y = naturalHeight/4;
  75.     toQuad[1].x = naturalWidth*9/8;
  76.     toQuad[1].y = 0;
  77.     toQuad[2].x = naturalWidth;
  78.     toQuad[2].y = naturalHeight*6/4;
  79.     toQuad[3].x = 0;
  80.     toQuad[3].y = naturalHeight;
  81.     SetIdentityMatrix( &matrix );
  82.     QuadToQuadMatrix( (Fixed *)fromQuad, (Fixed *)toQuad, &matrix );
  83.     err = GraphicsImportSetMatrix( importer, &matrix );
  84.     
  85.     // resize the window to fit, and redraw
  86.     // get the new bounds rect, size the window then draw
  87.     err = GraphicsImportGetBoundsRect( importer, &scaledBounds );
  88.     SizeWindow( window, scaledBounds.right, scaledBounds.bottom, false );
  89.     SetPortWindowPort( window );
  90.     EraseRect( &scaledBounds );
  91.     err = GraphicsImportDraw( importer );
  92.  
  93.     CloseComponent( importer );
  94. }
  95.